package com.sancorseguros.nova.selenium.pageobject;

import java.io.Serializable;

import org.openqa.selenium.WebDriver;

import com.sancorseguros.nova.selenium.config.StaticSelenium;
import com.thoughtworks.selenium.SeleneseTestBase;

public abstract class AbstractPage extends SeleneseTestBase implements IPage, Serializable {

	/** Serial Version ID */
	private static final long serialVersionUID = 732573635504039785L;

	private String title;
	private boolean errorPage;
	
	public AbstractPage() {}
	
	
	/**
	 * Verifica que la pagina cargada sea la esperada.
	 * @return verifyPage
	 */
	public boolean verifyPage(WebDriver driver, String title) {
		boolean verifyPage = false;
		verifyPage = driver.getTitle().toString().equals(title);
		return verifyPage;
	}
	
	/**
	 * Verifica que el texto este presente en la pagina cargada.
	 * @return textPresent
	 */
	protected boolean isTextPresent(WebDriver driver, String text) {
		try {
			boolean textPresent = driver.getPageSource().contains(text);
			return textPresent;
		} catch (Exception e) {
			return false;
		}
	}
	
	/**
	 * Espera por la carga de la pagina.
	 * @param title
	 * @throws InterruptedException
	 */
	public void waitForTitle(String title, String seleniumtitle) throws InterruptedException{
		for (int second = 0;; second++) {
			if (second >= 60) fail("timeout");
			try { if (title.equals(seleniumtitle))
				break; 
			} catch (Exception e) {
				System.out.println(StaticSelenium.INVALID_TITLE +" ("+e+")");
			}
			Thread.sleep(StaticSelenium.PAUSE_TIME);
		}
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public boolean isErrorPage() {
		return errorPage;
	}

	public void setErrorPage(boolean errorPage) {
		this.errorPage = errorPage;
	}
	
}
